$('body').on('click', '[class="pull-right fa fa-angle-left"]', function () {
    $(".bg-gray").removeClass("bg-gray");
    $(this).parent().addClass("bg-gray");
    $('[class="pull-right fa fa-angle-down"]').attr("class", "pull-right fa fa-angle-left");
    $('[name="B"]').remove();
    $('[name="C"]').remove();
    $(this).attr("class", "pull-right fa fa-angle-down");
    var url = "/b2c/shangpinfenlei/getLever";
    var bh = {"bh": $(this).attr("bh")};
    console.table($(this).attr("bh"))
    $.post(url, bh, function (json) {
        if (json.success) {
            var a = json.content;
            $.each(a, function (a, m) {
                var rjhtml =
                    "                        <div class=\"\" name=\"B\" style=\"padding:5px 5px 5px 15px\">\n" +
                    "                               <a name=\"bg-gray\" bh=\"" + m.mbh + "\">" + m.mmc + "</a>\n" +
                    "                               <a name=\"Get three levels of classification\" bh=\"" + m.mbh + "\" class=\"pull-right  fa fa-angle-left\"></a>\n" +
                    "                        </div>";
                var task = $(".bg-gray").after(rjhtml);
            });
        } else {
            alert(json.msg);
        }
    });
});


$('body').on('click', '[class="pull-right  fa fa-angle-left"]', function () {

    $(".bg-gray").removeClass("bg-gray");
    $(this).parent().addClass("bg-gray");
    $('[class="pull-right  fa fa-angle-down"]').attr("class", "pull-right  fa fa-angle-left");
    $('[name="C"]').remove();
    $(this).attr("class", "pull-right  fa fa-angle-down");
    var url = "/b2c/shangpinfenlei/getLever";
    var bh = {"bh": $(this).attr("bh")};
    console.table($(this).attr("bh"))
    $.post(url, bh, function (json) {
        if (json.success) {
            var a = json.content;
            $.each(a, function (a, m) {
                var rjhtml =
                    "                        <div class=\"\" name=\"C\" style=\"padding:5px 5px 5px 25px\">\n" +
                    "                            <div>" +
                    "                               <a name=\"bg-gray\" bh=\"" + m.mbh + "\">" + m.mmc + "</a>\n" +
                    "                            </div>" +
                    "                        </div>";
                var task = $(".bg-gray").after(rjhtml);
            });
        } else {
            alert(json.msg);
        }
    });
});


$('body').on('click', '[class="pull-right fa fa-angle-down"]', function () {
    $(".bg-gray").removeClass("bg-gray");
    $(this).parent().addClass("bg-gray");
    $(this).attr("class", "pull-right fa fa-angle-left");
    $('[name="B"]').remove();
    $('[name="C"]').remove();
});


$('body').on('click', '[class="pull-right  fa fa-angle-down"]', function () {
    $(".bg-gray").removeClass("bg-gray");
    $(this).parent().addClass("bg-gray");
    $(this).attr("class", "pull-right  fa fa-angle-left");
    $('[name="C"]').remove();
});



$('body').on('click', '[name="bg-gray"]', function () {
    $(".bg-gray").removeClass("bg-gray");
    $(this).parent().addClass("bg-gray");
    var url = "/b2c/shangpinfenlei/getForm";
    var bh = {"bh": $(this).attr("bh")};
    console.table($(this).attr("bh"));
    $.post(url, bh, function (json) {
        if (json.success) {
            var a = json.content;
            $.each(a, function (a, m) {
                var rjhtml =
                    "<form id=\"b2c-tree-from\" action=\"\" method=\"POST\">"+
                    "<div hidden=\"hidden\" class=\"form-group\">\n" +
                    "                        <input type=\"text\" id=\"mid\" class=\"form-control\" name=\"mid\"\n" +
                    "                               value=\"" + m.mid + "\" size=\"30\" data-validate=\"required:必填 \">\n" +
                    "                    </div>" +
                    "<div class=\"form-group\">\n" +
                    "                        <label for=\"mmc\" class=\"control-label\">名称:</label>\n" +
                    "                        <input type=\"text\" id=\"mmc\" class=\"form-control\" name=\"mmc\"\n" +
                    "                               value=\"" + m.mmc + "\" size=\"30\" data-validate=\"required:必填 \">\n" +
                    "                    </div>\n" +
                    "                    <div class=\"form-group\">\n" +
                    "                        <label for=\"mbh\" class=\"control-label\">编号:</label>\n" +
                    "                        <input type=\"text\" id=\"mbh\" class=\"form-control\" name=\"mbh\"\n" +
                    "                               value=\"" + m.mbh + "\" size=\"30\" data-validate=\"required:必填 \">\n" +
                    "                    </div>\n" +
                    "                    <div class=\"form-group\">\n" +
                    "                        <label for=\"jbiezhiduan\" class=\"control-label\">级别:</label>\n" +
                    "                        <input type=\"text\" id=\"jbiezhiduan\" class=\"form-control\" name=\"jbiezhiduan\"\n" +
                    "                               value=\"" + m.mbh.length / 4 + "级\" readonly=\"readonly\" size=\"30\"\n" +
                    "                               data-validate=\"required:必填 \">\n" +
                    "                    </div>\n" +
                    "                    <div class=\"form-group\">\n" +
                    "                        <label for=\"mcjsj\" class=\"control-label\">创建时间:</label>\n" +
                    "                        <input type=\"text\" id=\"mcjsj\" class=\"form-control\" name=\"mcjsj\"\n" +
                    "                               value=\"" + m.mcjsj + "\" readonly=\"readonly\" size=\"30\" data-validate=\"required:必填 \">\n" +
                    "                    </div>\n" +
                    "                    <button id=\"bc\" class=\"btn fa fa-save btn-primary\" style=\"border-radius:5px;\" type=\"button\"> 保存\n" +
                    "                    </button>\n" +
                    "                    <button id=\"sc\" class=\"btn fa fa-times-circle btn-danger\" style=\"border-radius:5px;\" type=\"button\">\n" +
                    "                        删除\n" +
                    "                    </button>\n" +
                    "                    <button id=\"cjxj\" class=\"btn fa fa-angle-down btn-warning\" style=\"border-radius:5px;\"> 创建下级\n" +
                    "                    </button>"+
                    "</form>";
                var task = $("#shangpingfenlei-from")[0].innerHTML = rjhtml;
            });
        } else {
            alert(json.msg);
        }
    });
});


$('body').on('click','[id="bc"]',function () {
    var data = new FormData($("#b2c-tree-from")[0]);
    $.ajax({
        url: "/b2c/shangpinfenlei/saveForm",
        type: "post",
        data: data,
        cache: false,
        processData: false,
        contentType: false,
        dataType: "json",
        success: function (events) {
            if (events===1){
                window.location.reload();
            } else {
                alert("保存失败")
            }

        }
    });
});


$('body').on('click','[id="sc"]',function () {
    var data = new FormData($("#b2c-tree-from")[0]);
    $.ajax({
        url: "/b2c/shangpinfenlei/delForm",
        type: "post",
        data: data,
        cache: false,
        processData: false,
        contentType: false,
        dataType: "json",
        success: function (events) {
            if (events===1){
                window.location.reload();
            } else {
                alert("删除失败")
            }

        }
    });
});


$(function () {
    var url = "/b2c/shangpinfenlei/getLever";
    $.post(url, function (json) {
        if (json.success) {
            var a = json.content;
            $.each(a, function (a, m) {
                var rjhtml =
                    "<div class=\"\" style=\"padding:5px 5px 5px 5px;\">\n" +
                    "                             <a   name=\"bg-gray\" bh=\"" + m.mbh + "\" >" + m.mmc + "</a>\n" +
                    "                             <a bh=\"" + m.mbh + "\" class=\"pull-right fa fa-angle-left\"></a>\n" +
                    "                    </div>";
                var task = $(".bg-gray").after(rjhtml);
                if (a === 0) {
                    var rjhtml =
                        "<div class=\"bg-gray\" style=\"padding:5px 5px 5px 5px;\">\n" +
                            "                             <a   name=\"bg-gray\" bh=\"" + m.mbh + "\" >" + m.mmc + "</a>\n" +
                            "                             <a bh=\"" + m.mbh + "\" class=\"pull-right fa fa-angle-left\"></a>\n" +
                            "                    </div>";
                    var task = $("#tree_shangpinfenlei")[0].innerHTML = rjhtml;

                    var formHtml =
                        "<form id=\"b2c-tree-from\" action=\"\" method=\"POST\">" +
                        "<div hidden=\"hidden\" class=\"form-group\">\n" +
                        "                        <input type=\"text\" id=\"mid\" class=\"form-control\" name=\"mid\"\n" +
                        "                               value=\"" + m.mid + "\" size=\"30\" data-validate=\"required:必填 \">\n" +
                        "                    </div>" +
                        "<div class=\"form-group\">\n" +
                        "                        <label for=\"mmc\" class=\"control-label\">名称:</label>\n" +
                        "                        <input type=\"text\" id=\"mmc\" class=\"form-control\" name=\"mmc\"\n" +
                        "                               value=\"" + m.mmc + "\" size=\"30\" data-validate=\"required:必填 \">\n" +
                        "                    </div>\n" +
                        "                    <div class=\"form-group\">\n" +
                        "                        <label for=\"mbh\" class=\"control-label\">编号:</label>\n" +
                        "                        <input type=\"text\" id=\"mbh\" class=\"form-control\" name=\"mbh\"\n" +
                        "                               value=\"" + m.mbh + "\" size=\"30\" data-validate=\"required:必填 \">\n" +
                        "                    </div>\n" +
                        "                    <div class=\"form-group\">\n" +
                        "                        <label for=\"jbiezhiduan\" class=\"control-label\">级别:</label>\n" +
                        "                        <input type=\"text\" id=\"jbiezhiduan\" class=\"form-control\" name=\"jbiezhiduan\"\n" +
                        "                               value=\"" + m.mbh.length / 4 + "级\" readonly=\"readonly\" size=\"30\"\n" +
                        "                               data-validate=\"required:必填 \">\n" +
                        "                    </div>\n" +
                        "                    <div class=\"form-group\">\n" +
                        "                        <label for=\"mcjsj\" class=\"control-label\">创建时间:</label>\n" +
                        "                        <input type=\"text\" id=\"mcjsj\" class=\"form-control\" name=\"mcjsj\"\n" +
                        "                               value=\"" + m.mcjsj + "\" readonly=\"readonly\" size=\"30\" data-validate=\"required:必填 \">\n" +
                        "                    </div>\n" +
                        "                    <button id=\"bc\" class=\"btn fa fa-save btn-primary\" style=\"border-radius:5px;\" type=\"button\"> 保存\n" +
                        "                    </button>\n" +
                        "                    <button id=\"sc\" class=\"btn fa fa-times-circle btn-danger\" style=\"border-radius:5px;\" type=\"button\">\n" +
                        "                        删除\n" +
                        "                    </button>\n" +
                        "                    <button id=\"cjxj\" class=\"btn fa fa-angle-down btn-warning\" style=\"border-radius:5px;\"> 创建下级\n" +
                        "                    </button>" +
                        "</form>";
                    var task = $("#shangpingfenlei-from")[0].innerHTML = formHtml;
                }
            });
        } else {
            alert(json.msg);
        }
    });
});